package com.shopping.service;

import com.shopping.dao.OrderMapper;
import com.shopping.entity.MiaoshaOrder;
import com.shopping.entity.OrderInfo;
import com.shopping.entity.User;
import com.shopping.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MiaoshaService miaoshaService;

    public MiaoshaOrder getMiaoshaOrderByOrderIdGoodsId(Long userId,Long goodsId) {
        return orderMapper.getMiaoshaOrderByOrderIdGoodsId(userId,goodsId);
    }

    @Transactional
    public OrderInfo createOrder(User user, GoodsVO goodsVOByGoodsId) {
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setCreateDate(LocalDateTime.now());
        orderInfo.setDeliveryAddrId(0);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goodsVOByGoodsId.getId());
        orderInfo.setGoodsName(goodsVOByGoodsId.getGoodsName());
        orderInfo.setGoodsPrice(goodsVOByGoodsId.getMiaoshaPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        long orderId=orderMapper.insert(orderInfo);

        MiaoshaOrder miaoshaOrder=new MiaoshaOrder();
        miaoshaOrder.setOrderId(orderId);
        miaoshaOrder.setGoodsId(goodsVOByGoodsId.getId());
        miaoshaOrder.setUserId(user.getId());
        orderMapper.insertMiaoshaOrder(miaoshaOrder);

        return orderInfo;
    }
}
